package data;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Medicament;
import bean.Prescription;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ServletPDF extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ServletPDF() {
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String medecin = (String) request.getParameter("medecin");
			String date = (String) request.getParameter("date");
			String patient = (String) request.getParameter("patient");
			Integer id = Integer.parseInt(request.getParameter("idOrdonnance"));
			ArrayList<Prescription> listePrescription = Rmi
					.recherchePrescription(id, 0, 100);
			;

			BufferedOutputStream out = new BufferedOutputStream(
					response.getOutputStream());
			Document document = new Document();
			PdfWriter.getInstance(document, out);
			document.open();
			document.addTitle("ordonnance.pdf");
			document.newPage();
			document.add(new Paragraph("Dr. " + medecin));
			document.add(new Paragraph("Le " + date));
			document.add(new Paragraph("Pour le patient :" + patient));
			for (Prescription p : listePrescription) {
				Medicament medoc = Rmi.getMedicament(p.getIdMedicament());
				document.add(new Paragraph("Medicament : " + medoc.getNom()+" - "+p.getPosologie()));
			}
			document.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
